package Transforming_Observables

import (
	"context"
	"fmt"
	"github.com/reactivex/rxgo/v2"
)

// 可以用作累加器等等
// apply a function to each item emitted by an Observable, sequentially, and emit each successive value
// 官方者话很难理解

func Demo_Scan() {
	fmt.Println("Demo_BufferWithCount###########################")
	observable := rxgo.Just(1, 2, 3, 4, 5)().
		Scan(func(_ context.Context, acc interface{}, elem interface{}) (interface{}, error) {
			if acc == nil {
				return elem, nil
			}
			return acc.(int) + elem.(int), nil
		})

	for item := range observable.Observe() {
		if item.Error() {
			fmt.Println("订阅：", item.E)
		}
		fmt.Println("订阅：", item.V)
	}
}
